const { translateTower, translateValue } = require('../../display/general')
const { ignoreIt } = require('../../util/ignore')
const { getModelType } = require('../../util/utils')
const { parseTowerBehavior } = require('./behavior')
const { parseGeraldoItems } = require('./geraldo')

const parseFootprint = (footprint) => {
    if (typeof footprint.radius !== 'undefined') {
        return String(footprint.radius)
    }
    return footprint.xWidth + '×' + footprint.yWidth
}

function parseTower(tower) {
    let result = {
        name: translateTower(tower.name),
        range: tower.range,
        footprint: parseFootprint(tower.footprint),
        attacks: [],
        abilities: [],
        buffs: [],
        zones: [],
        subTowers: [],
        meta: {},
        shopItems: [], // geraldo shop
        spells: [], // corvus spell
        behaviors: [],
        notes: [
            tower.footprint.doesntBlockTowerPlacement ? translateValue('doestBlockTowerPlacement', 'does not block tower placement') : null,
        ],
    };
    tower.behaviors.forEach(b => {
        let type = getModelType(b);
        if (ignoreIt(type)) {
            return
        }
        if (type == 'AddBehaviorToTowerTypeSupportModel' && tower.baseId == 'Silas') {
            return
        }
        parseTowerBehavior(b, result, type)
    });
    mergeAttacks(result, tower)
    if (tower.baseId === 'Geraldo') {
        result.shopItems = parseGeraldoItems(tower.tier)
    }
    return result;
}

function mergeAttacks(tower, og) {
    if (tower.isSubTower) return
    // buccaneer paragon
    if (og.baseId === 'MonkeyBuccaneer' && og.tier === 6) {
        let attacks = []
        attacks.push(tower.attacks[0]) // cannon
        attacks.push(tower.attacks[1]) // grape
        attacks.push(tower.attacks[12]) // plane spawner
        attacks.push(tower.attacks[13]) // hooks
        attacks[0].numberOfAttack = 6
        attacks[1].numberOfAttack = 6
        attacks[3].numberOfAttack = 10
        tower.attacks = attacks
        return
    }
    // mermonkey 3xx+
    if (og.baseId === 'Mermonkey' && og.tiers[0] >= 3) {
        let attacks = []
        attacks.push(tower.attacks[0]) // trident
        attacks.push(tower.attacks[2]) // tentacle
        attacks[1].numberOfAttack = 8
        tower.attacks = attacks
        return
    }
    // engineer paragon
    if (og.baseId === 'EngineerMonkey' && og.tier === 6) {
        tower.attacks = [tower.attacks[0]]
        tower.attacks[0].numberOfAttack = 3
        return
    }
    // spactory x4x+
    if (og.baseId === 'SpikeFactory' && og.tiers[1] >= 4 && og.tier < 6) {
        tower.abilities[0].attacks[0].numberOfAttack = 4
        tower.abilities[0].attacks = [tower.abilities[0].attacks[0]]
        return
    }
    // super x3x+
    if (og.baseId === 'SuperMonkey' && og.tiers[1] >= 3) {
        tower.attacks.pop()
        tower.attacks[0].numberOfAttack = 2
        return
    }
    // buccaneer non-paragons
    if (og.baseId === 'MonkeyBuccaneer') {
        let attacks = []
        tower.attacks.forEach(a => {
            if (a.meta.isAlternate) {
                return
            }
            if (!a.projectile[0].meta.subTowers) {
                a.numberOfAttack = 2
            }
            attacks.push(a)
        })
        tower.attacks = attacks
        return
    }
    // dartling xx4+
    if (og.baseId === 'DartlingGunner' && og.tiers[2] >= 4) {
        let number = tower.attacks.length
        tower.attacks = [tower.attacks[0]]
        tower.attacks[0].numberOfAttack = number
        return
    }
    // corvus
    if (og.baseId === 'Corvus') {
        tower.attacks = [tower.attacks[0]]
        tower.subTowers[0].attacks = [tower.subTowers[0].attacks[0]]
    }
}

module.exports = {
    parseTower
}